package com.tyk.bookstore.back.task.schedule;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author tyk
 * @description TaskHelper
 */
@Service
@RequiredArgsConstructor
public class TaskHelper {
    private Map<TaskEnum, Runnable> taskMap;

    private final BookTask bookTask;

    /**
     * 初始化任务
     */
    @PostConstruct
    private void init() {
        taskMap = Map.of(
                TaskEnum.BOOK_COLLECT_NUM, bookTask::flushCollectNum,
                TaskEnum.BOOK_COMMENT_NUM, bookTask::flushCommentNum,
                TaskEnum.BOOK_PAY_NUM, bookTask::flushPayNum,
                TaskEnum.BOOK_SALE_NUM, bookTask::flushSaleNum,
                TaskEnum.BOOK_LIKE_NUM, bookTask::flushLikeNum
        );
    }

    /**
     * 根据任务名称获取任务
     *
     * @param type 任务类型
     * @return 任务
     */
    public Runnable getTask(TaskEnum type) {
        return taskMap.get(type);
    }
}
